package com.java;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.Date;

import com.service.LiuYan;
import com.sun.crypto.provider.RSACipher;

public class Data {
	Connection con=null;
	public int dataInsert(LiuYan a){
		
		//生成连接对象
		con=DataConnection.getConnection();
		
		String sql="insert into LiuYan(id,userName,tel,email,content,time) values(myseq.nextval,?,?,?,?,?)";
		//PreparedStatemnt表示预编译的sql语句的对象
		PreparedStatement pst=null;
		//定义aa，用来表示执行PreparedStatement的结果，最后return aa;
		int aa=0;

		
		
		try {
			
			//生成语句对象
			pst=con.prepareStatement(sql);
			//获得javabean中的数据
			pst.setString(1,a.getUserName());
			pst.setString(2,a.getTel());
			pst.setString(3,a.getEmail());
			pst.setString(4,a.getContent());
			//时间戳的数据类型应该用long
			pst.setLong(5,new Date().getTime());
			//Integer.parseInt(String.valueOf((new Date().getTime())))
			
			aa=pst.executeUpdate();
			//输出结果是1，即executeUpdate()执行后的返回值是1
			//System.out.println(aa);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//执行sql语句
		return aa;
	}
	/**
	 * 全部留言查询
	 */
	public ResultSet getLiuyan() {
		String sql="select * from liuyan";
		con=DataConnection.getConnection();
		ResultSet rs=null;
		try {
			PreparedStatement st=con.prepareStatement(sql);
			rs=st.executeQuery();
			//释放资源,释放查询到的结果并关闭当前连接
			//rs.close();
			//st.close();
			//con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;
	}
	
	/**
	 * 根据id查询留言
	 * @param i
	 * @return
	 */
	public ResultSet getLiuyanByid(String i) {
		String sql="select * from liuyan where id=?";
		con=DataConnection.getConnection();
		ResultSet rs=null;
		try {
			PreparedStatement st=con.prepareStatement(sql);
			st.setString(1, i);
			rs=st.executeQuery();
			//释放资源,释放查询到的结果并关闭当前连接
			//rs.close();
			//st.close();
			//con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs;
	}
	
	
	//判断是否删除成功，需要靠返回值进行判断，return_type需为int
	public int delete(int i) {
		//定义返回对象 ，aa=st.executeUpdate();
		int aa=0;
		//删除某条数据应该是delete from 表名,没有*
		String sql="delete from LiuYan where id=?";
		con=DataConnection.getConnection();
		PreparedStatement st=null;
		try {
			st=con.prepareStatement(sql);
			st.setInt(1,i);
			
			aa=st.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return aa;
	}
	
	public int dataUpdate(LiuYan ly,String id) {
		String sql="update liuyan set userName=?,tel=?,email=?,content=?,time=? where id=?";
		PreparedStatement pst=null;
		Connection con=null;
		int aa=0;
		con=DataConnection.getConnection();
		
		try {
			pst=con.prepareStatement(sql);
			pst.setString(1, ly.getUserName());
			pst.setString(2, ly.getTel());
			pst.setString(3, ly.getEmail());
			pst.setString(4, ly.getContent());
			pst.setLong(5, new Date().getTime());
			pst.setString(6, id);
			
			aa=pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return aa;
	}
	public static void main(String[] args) {
/*		//不能直接使用本类中的方法，需先调用本类
		Data aa=new Data();
		
		//测试查询
		ResultSet rs=aa.getLiuyan();
		//移动指针到下一条目录
		try {
			if (rs.next()) {
				System.out.println(rs.getString("userName"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/

	}
}
